/*
 * Copyright 2010 WithOne Co. Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 *     
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package jp.co.withone.osgi.gadget.controller;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
import javax.swing.JTree;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreePath;

import jp.co.withone.osgi.gadget.controller.preference.Preference;

import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import org.osgi.util.tracker.ServiceTrackerCustomizer;

/**
 * @author t.funakky
 */
class PreferenceManager implements ActionListener, ServiceTrackerCustomizer, TreeSelectionListener {

    private JFrame view;
    private Vector<PreferenceWrapper> wrapperList;
    private final BundleContext context;
    private JSplitPane splitpane;

    PreferenceManager(BundleContext context) {
        this.context = context;
        this.wrapperList = new Vector<PreferenceWrapper>();
    }

    @Override
    public synchronized void actionPerformed(ActionEvent event) {
        if (this.view != null && this.view.isVisible()) {
            this.view.setVisible(true);
            return;
        }
        if (this.view != null && !this.view.isVisible()) {
            this.view.dispose();
        }
        this.view = new JFrame();
        JPanel page = this.wrapperList.get(0).preference.getPage();
        JTree tree = new JTree(this.wrapperList);
        tree.addTreeSelectionListener(this);
        this.splitpane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, tree, page);
        this.view.getContentPane().add(splitpane, BorderLayout.CENTER);
        //        this.view.pack();
        this.view.setSize(500, 300);

        ImageIcon icon = new ImageIcon(this.getClass().getResource(ControllerCore.RESOURCE_TRAY_ICON));
        this.view.setIconImage(icon.getImage());
        this.view.setVisible(true);
    }

    @Override
    public synchronized Object addingService(ServiceReference ref) {
        String name = (String) ref.getProperty(Preference.NAME);
        String parent = (String) ref.getProperty(Preference.PARENT);
        String[] children = (String[]) ref.getProperty(Preference.CHILDREN);
        Preference preference = (Preference) this.context.getService(ref);
        PreferenceWrapper wrapper = new PreferenceWrapper(name, preference, parent, children);
        this.wrapperList.add(wrapper);
        return wrapper.preference;
    }

    @Override
    public void modifiedService(ServiceReference ref, Object service) {
    }

    @Override
    public synchronized void removedService(ServiceReference ref, Object service) {
        for (PreferenceWrapper wrapper : this.wrapperList) {
            if (wrapper.preference == service) {
                this.wrapperList.remove(wrapper);
                break;
            }
        }
    }

    @Override
    public void valueChanged(TreeSelectionEvent event) {
        TreePath path = event.getNewLeadSelectionPath();
        DefaultMutableTreeNode node = (DefaultMutableTreeNode) path.getLastPathComponent();
        PreferenceWrapper wrapper = (PreferenceWrapper) node.getUserObject();
        this.splitpane.setRightComponent(wrapper.preference.getPage());
    }

    private static class PreferenceWrapper extends DefaultMutableTreeNode {

        private static final long serialVersionUID = -7708215299884438714L;
        final String name;
        final Preference preference;
        final String parent;
        final String[] children;

        PreferenceWrapper(String name, Preference preference, String parent, String[] children) {
            this.name = name;
            this.preference = preference;
            this.parent = parent;
            this.children = children;
            super.userObject = name;
        }
    }
}
